package com.example.home.viewmodel;

import android.util.Log;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.GameCtrlCom;
import com.example.common.component.ImgLoaderCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.GameApiGetGameResp;
import com.example.common.model.GameResp;
import com.example.common.user.GlobalUser;
import com.example.home.model.Card;
import com.example.home.model.RecommendCard;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;


public class HomeRecommendViewModel extends ViewModel {
    // TODO: Implement the ViewModel
    private static final String TAG="HomeRecommendViewModel";

    private GameCtrlCom gameCtrlCom;

    public static int LOAD_NUMBER=10;
    public boolean isFirstLoad=true;

    private LinkedList<GameResp> games=new LinkedList<>();
    private MutableLiveData<Boolean> isLoadDone=new MutableLiveData<>();//加载是否完成
    private MutableLiveData<Boolean> isLoadDownDone=new MutableLiveData<>();//加载是否完成
    private MutableLiveData<String> remainMsg=new MutableLiveData<>();//提示信息

    public HomeRecommendViewModel(){
        //加载数据
        init();
    }

    private void init(){
        gameCtrlCom=new GameCtrlCom();
    }

    /**
     * 下滑刷新——数据加载错误时有处理的版本（后期可用，现在不改）
     */
    public void loadTop(){

        gameCtrlCom.getGameRecommend(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if(response!=null){
                    List<GameResp> resp=(List<GameResp>)response;
                    games.addAll(0,resp);
                    //加载了多少个
                    LOAD_NUMBER=resp.size();
                    isLoadDone.setValue(true);
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 上滑加载
     */
    public void loadDown(){

        gameCtrlCom.getGameRecommend(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if(response!=null){
                    List<GameResp> resp=(List<GameResp>)response;
                    games.addAll(resp);
                    //加载了多少个
                    LOAD_NUMBER=resp.size();
                    isLoadDownDone.setValue(true);
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 收藏一个游戏
     * @param id 游戏id
     */
    public void addFavor(Integer id){
        gameCtrlCom.postGameIdFavor(id, GlobalUser.TOKEN, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                GlobalUser.getInstance().getLikeMap().put(id,(Boolean)response);
                Log.i(TAG, "onSuccess: 收藏成功");
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 取消一个游戏收藏
     * @param id 游戏id
     */
    public void deleteFavor(Integer id){
        gameCtrlCom.deleteGameIdFavor(id, GlobalUser.TOKEN, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if(GlobalUser.getInstance().getLikeMap().get(id)!=null){
                    GlobalUser.getInstance().getLikeMap().remove(id);
                }
                Log.i(TAG, "onSuccess: 取消收藏成功");
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    public MutableLiveData<String> getRemainMsg() {
        return remainMsg;
    }

    public LinkedList<GameResp> getGames() {
        return games;
    }

    public MutableLiveData<Boolean> getIsLoadDone() {
        return isLoadDone;
    }

    public MutableLiveData<Boolean> getIsLoadDownDone() {
        return isLoadDownDone;
    }
}